<?php
declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\AdminZccandidate as M;
use app\common\model\AdminZavote as MA;
use app\common\model\AdminZdvoter as MD;
use app\common\validate\AdminZccandidate as V;
use think\facade\Db;
use think\facade\Cache;

class AdminZccandidate
{
    // 添加
    public static function goAdd($data)
    {
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
            M::create($data);
            $res = M::where('biaoid', $data['biaoid'])->where('shenhe', 0)->count();
            MA::where('id', $data['biaoid'])->update(['zonghouxuan' => $res]);
            Cache::delete('tpgettpoptionybypid:'.$data['biaoid']); 
            
            $resma = MA::find($data['biaoid']);
            if($resma['fenzu'] == 1){
                if($resma['fenzuname']){
                    $grouparr=explode('/',$resma['fenzuname']);
                    $grouparr=array_filter($grouparr);
                    foreach($grouparr as $key=>$value){
                        for($i=0;$i<50;$i++){
                            Cache::delete('tpgettpoptionebypidjia:'.'youfenzu'.$value.$data['biaoid'].$i); 

                            Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].$i); 
                        }

                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'yi'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'er'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'san'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'zong'); 
                    }
                }
            }

            for($i=0;$i<50;$i++){
                Cache::delete('tpgettpoptionycontentbypid:'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypid:'.'wufenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypid:'.'youfenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypidjia:'.'wufenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypidjia:'.'youfenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionesoubypid:'.'wufenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionesoubypid:'.'youfenzu'.$data['biaoid'].$i); 

                Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].$i); 
            }

            

            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'yi'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'er'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'san'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'zong'); 
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id)
    {
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
             M::update($data);
            $res = M::where('biaoid', $data['biaoid'])->where('shenhe', 0)->count();
            MA::where('id', $data['biaoid'])->update(['zonghouxuan' => $res]);
            Cache::delete('tpgettpoptionybypid:'.$data['biaoid']); 
            
            $resma = MA::find($data['biaoid']);
            if($resma['fenzu'] == 1){
                if($resma['fenzuname']){
                    $grouparr=explode('/',$resma['fenzuname']);
                    $grouparr=array_filter($grouparr);
                    foreach($grouparr as $key=>$value){
                        for($i=0;$i<50;$i++){
                            Cache::delete('tpgettpoptionebypidjia:'.'youfenzu'.$value.$data['biaoid'].$i); 

                            Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].$i); 
                        }

                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'yi'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'er'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'san'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$data['biaoid'].'zong'); 
                    }
                }
            }

            for($i=0;$i<50;$i++){
                Cache::delete('tpgettpoptionycontentbypid:'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypid:'.'wufenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypid:'.'youfenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypidjia:'.'wufenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypidjia:'.'youfenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionesoubypid:'.'wufenzu'.$data['biaoid'].$i); 
                Cache::delete('tpgettpoptionesoubypid:'.'youfenzu'.$data['biaoid'].$i); 

                Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].$i); 
            }

            Cache::delete('tpgettpoptionbypid:'.$id); 
            Cache::delete('tpgettpoptionbypid:'.$id.'er'); 

            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'yi'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'er'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'san'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$data['biaoid'].'zong'); 
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id)
    {
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save([
                'status' => $data,
            ]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id)
    {
        $model = M::find($id);
        if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->delete();
            $res = M::where('biaoid', $model['biaoid'])->where('shenhe',0)->count();
            MA::where('id', $model['biaoid'])->update(['zonghouxuan' => $res]);
            Cache::delete('tpgettpoptionybypid:'.$model['biaoid']); 
            
            $resma = MA::find($model['biaoid']);
            if($resma['fenzu'] == 1){
                if($resma['fenzuname']){
                    $grouparr=explode('/',$resma['fenzuname']);
                    $grouparr=array_filter($grouparr);
                    foreach($grouparr as $key=>$value){
                        for($i=0;$i<50;$i++){
                            Cache::delete('tpgettpoptionebypidjia:'.'youfenzu'.$value.$model['biaoid'].$i); 

                            Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$model['biaoid'].$i); 
                        }

                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$model['biaoid'].'yi'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$model['biaoid'].'er'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$model['biaoid'].'san'); 
                        Cache::delete('tpgettpoptionrankingbypid:'.'youfenzu'.$value.$model['biaoid'].'zong'); 
                    }
                }
            }

            for($i=0;$i<50;$i++){
                Cache::delete('tpgettpoptionycontentbypid:'.$model['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypid:'.'wufenzu'.$model['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypid:'.'youfenzu'.$model['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypidjia:'.'wufenzu'.$model['biaoid'].$i); 
                Cache::delete('tpgettpoptionebypidjia:'.'youfenzu'.$model['biaoid'].$i); 
                Cache::delete('tpgettpoptionesoubypid:'.'wufenzu'.$model['biaoid'].$i); 
                Cache::delete('tpgettpoptionesoubypid:'.'youfenzu'.$model['biaoid'].$i); 

                Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$model['biaoid'].$i); 
            }

            Cache::delete('tpgettpoptionbypid:'.$id); 
            Cache::delete('tpgettpoptionbypid:'.$id.'er'); 

            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$model['biaoid'].'yi'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$model['biaoid'].'er'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$model['biaoid'].'san'); 
            Cache::delete('tpgettpoptionrankingbypid:'.'wufenzu'.$model['biaoid'].'zong'); 
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids,$biaoid)
    {
        if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
        try{
            M::destroy($ids);
            $res = M::where('biaoid', $biaoid)->where('shenhe',0)->count();
            MA::where('id', $biaoid)->update(['zonghouxuan' => $res]);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle()
    {
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = input('get.limit');
        
               //按名称查找
               if ($name = input("name")) {
                   $where[] = ["name", "like", "%" . $name . "%"];
               }
               //按编号查找
               if ($bianhao = input("bianhao")) {
                   $where[] = ["bianhao", "like", "%" . $bianhao . "%"];
               }
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }

    // 未开始进行中已结束
    public static function goWjy($biaoid)
    {
        $res=Db::name("admin_zavote")->find($biaoid);
        $nowtime = date("Y-m-d H:m:s");
        if($nowtime < $res['begintime']){
            $wjy = 0;
        }elseif($nowtime > $res['stoptime']){
            $wjy = 2;
        }else{
            $wjy = 1;
        }
        return ['msg'=>'操作成功','code'=>200,'wjy'=>$wjy];
    } 

    // 编号
    public static function goBianhaofenzu($biaoid)
    {
        $bianhao = M::where('biaoid', $biaoid)->max('bianhao');
        $res = MA::where('id', $biaoid)->field('fenzu,fenzuname')->find();
        return ['msg'=>'操作成功','code'=>200,'bianhao'=>$bianhao,'fenzu'=>$res['fenzu'],'fenzuname'=>$res['fenzuname']];
    }  

    // 状态
    public static function goState($id)
    {
        $res = M::find($id);
        if($res['ztdp'] == 0){
            M::where('id', $id)->update(['ztdp' => '1']);
        }elseif($res['ztdp'] == 1){
            M::where('id', $id)->update(['ztdp' => '0']);
        }
        return ['msg'=>'操作成功','code'=>200];
    }  

    // 改票
    public static function goGaipiao($id,$numpiao)
    {
        M::where('id', $id)->update(['numpiao' => $numpiao]);
        $res = M::find($id);
        $resnum = M::where('biaoid', $res['biaoid'])->sum('numpiao');
        MA::where('id', $res['biaoid'])->update(['sumpiao' => $resnum]);
        return ['msg'=>'操作成功','code'=>200];
    }  

    // 导出数据
    public static function goGetdata($biaoid,$daochufenzu)
    {
        // $url = "http://thtpadmin.diandianx.com/";
        $url = "http://thtpadmin.xingtaiwang.net/";
        if($daochufenzu == 'wfenzu'){
            if($biaoid == 119){
                $res = M::where('biaoid', $biaoid)->field('name,bianhao,video,numpiao,cat,xingming,zubie,create_time')->select();
                foreach($res as $key=>$value){
                    $res[$key]['videourl']=$url.$value['video'];
                }
            }else{
                $res = M::where('biaoid', $biaoid)->field('name,bianhao,tel,numpiao,cat,create_time')->select();
            }
        }else{
            $where = [];
            if($daochufenzu){
                $where[] = ["group", "like", $daochufenzu];
            }
            $where[] = ["biaoid", "like", $biaoid];
            $res = M::onlyTrashed()->where($where)->field('name,bianhao,tel,group,numpiao,cat,create_time')->select();
        }
        $restitle = MA::where('id', $biaoid)->field('title')->find();
        return ['msg'=>'操作成功','code'=>200,'data'=>$res,'datatitle'=>$restitle];
    }  

    // 得票统计
    public static function goDepiaotongji($id)
    {
        $jinripiaoyi = MD::where('xiangid', $id )->where('riqi', date("Y-m-d",time()))->sum('cishu');
        $jinripiaoer = MD::where('xiangid', "like", "%".",".$id )->where('riqi', date("Y-m-d",time()))->sum('cishu');
        $jinripiaosan = MD::where('xiangid', "like", $id.","."%" )->where('riqi', date("Y-m-d",time()))->sum('cishu');
        $jinripiaosi = MD::where('xiangid', "like", "%".",".$id.","."%" )->where('riqi', date("Y-m-d",time()))->sum('cishu');
        $jinripiao = $jinripiaoyi + $jinripiaoer + $jinripiaosan + $jinripiaosi;
        
        
        $riqi = strtotime(date("Y-m-d",time()));
        
        $time[0]=$riqi;
        for($i=1;$i<13;$i++){
            if($i==1){
                $time[$i]=$riqi+7200;
            }else{
                $time[$i]=$time[$i-1]+7200;
            }
            $a=$time[$i-1];
            $b=$time[$i];
            $yi[$i]=MD::where('xiangid', $id )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');
            $er[$i]=MD::where('xiangid', "like", "%".",".$id )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');
            $san[$i]=MD::where('xiangid', "like", $id.","."%" )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');
            $si[$i]=MD::where('xiangid', "like", "%".",".$id.","."%" )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');

            $count[$i] = $yi[$i] + $er[$i] + $san[$i] + $si[$i];
        }
        // $count = array(320, 332, 301, 334, 390, 330, 320, 230, 1560, 156, 156, 156);
        return ['msg'=>'操作成功','code'=>200,'jinripiao'=>$jinripiao,'shuju'=>"$count[1], $count[2], $count[3], $count[4], $count[5], $count[6], $count[7], $count[8], $count[9], $count[10], $count[11], $count[12]"];
    }  

    // 得票统计数据
    public static function goDepiaotongjishuju($id,$data)
    {
        $jinripiaoyi = MD::where('xiangid', $id )->where('riqi', $data)->sum('cishu');
        $jinripiaoer = MD::where('xiangid', "like", "%".",".$id )->where('riqi', $data)->sum('cishu');
        $jinripiaosan = MD::where('xiangid', "like", $id.","."%" )->where('riqi', $data)->sum('cishu');
        $jinripiaosi = MD::where('xiangid', "like", "%".",".$id.","."%" )->where('riqi', $data)->sum('cishu');
        $jinripiao = $jinripiaoyi + $jinripiaoer + $jinripiaosan + $jinripiaosi;
        
        
        $riqi = strtotime($data);
        
        $time[0]=$riqi;
        for($i=1;$i<13;$i++){
            if($i==1){
                $time[$i]=$riqi+7200;
            }else{
                $time[$i]=$time[$i-1]+7200;
            }
            $a=$time[$i-1];
            $b=$time[$i];
            $yi[$i]=MD::where('xiangid', $id )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');
            $er[$i]=MD::where('xiangid', "like", "%".",".$id )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');
            $san[$i]=MD::where('xiangid', "like", $id.","."%" )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');
            $si[$i]=MD::where('xiangid', "like", "%".",".$id.","."%" )->where([['datetime','>',$a],['datetime','<',$b]])->sum('cishu');

            $count[$i] = $yi[$i] + $er[$i] + $san[$i] + $si[$i];
        }
        $count = array($count[1], $count[2], $count[3], $count[4], $count[5], $count[6], $count[7], $count[8], $count[9], $count[10], $count[11], $count[12]);
        return ['msg'=>'操作成功','code'=>200,'jinripiao'=>$jinripiao,'shuju'=>$count];
    }  

    // 清空数据
    public static function goQingkongshuju($id)
    {
        $data = M::where('biaoid', $id)->select();
        foreach ($data as $k => $v) {
            $da['id'] = $v['id'];
            $da['numpiao'] = 0;
            $da['zhennumpiao'] = 0;
            $da['cat'] = 0;
            Db::name("admin_zccandidate")->update($da);
        }
        MA::where('id', $id)->update(['sumpiao' => '0']);
        MA::where('id', $id)->update(['todaypiao' => '0']);
        MA::where('id', $id)->update(['todaytime' => '0']);
        $whe['biaoid']=$id;
        Db::name("admin_zdvoter")->where($whe)->delete();
        Db::name("admin_zbuserinfo")->where($whe)->delete();

        Cache::tag(['tpyonghutousum','tpyonghutouzusum'])->clear();
        return ['msg'=>'操作成功','code'=>200];
    }  
    
}
